package com.ctbri.vSearch.dao.es.multiThread;

import com.ctbri.vSearch.frame.RedisPooledConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.stereotype.Repository;

import com.ctbri.vSearch.bean.po.ArticleItem;
import com.ctbri.vSearch.util.TimeUtil;
import redis.clients.jedis.Jedis;


public class EsQueryThread implements Runnable{
	
	
	private CountDownLatch cdl = null;
	private ConcurrentHashMap<Integer, List<ArticleItem>> concurmap = null;
	private String key =null;
	private String word =null;
	private Double weight =null;
	
	private ESSearchDao articleItemSearchDao = null;
	private Jedis client = null;
	private RedisPooledConnection rpc = null;
	
	Map<ArticleItem,Double> articleScore =null;
	Map<ArticleItem, HashMap<String, Float>> termScore =null;
	private AtomicInteger atomcCount = null;
	
	public EsQueryThread(){
		
	}
	/***
	 * 初始化查询
	 * @param atomcCount 传入自增变量 
	 * @param articleItemSearchDao 使用的es查询服务
	 * @param key 对es的查询范围
	 * @param word 查询词
	 * @param cdl  CountDownLatch
	 * @param concurmap 用于记录查询结果key为 atomcCount
	 */
	public EsQueryThread(AtomicInteger atomcCount, ESSearchDao articleItemSearchDao ,String key,String word,Double weight,CountDownLatch cdl,ConcurrentHashMap<Integer, List<ArticleItem>> concurmap,
			Map<ArticleItem,Double> articleScore,Map<ArticleItem, HashMap<String, Float>> termScore, Jedis client, RedisPooledConnection rpc){
		this.cdl = cdl;
		this.concurmap = concurmap;
		this.key = key;
		this.word = word;
		this.weight = weight;
		this.articleItemSearchDao = articleItemSearchDao;
		this.atomcCount = atomcCount;
		this.articleScore = articleScore;
		this.termScore = termScore;
		this.client = client;
		this.rpc = rpc;
	}
	
	
	@Override
	public void run() {
		TimeUtil timeutil = new TimeUtil();
		long start = timeutil.startTime();
		//获取连接并查询
		List<ArticleItem> letts = articleItemSearchDao.searchArticle(articleScore,termScore,key, word, weight, client);

		long end = timeutil.stopTime();
		System.out.println("es单词查询："+(end - start));
		cdl.countDown();
		rpc.returnResource(client);
	}

}
